#ifndef CLOUDCONTROLLER_H
#define CLOUDCONTROLLER_H

#include <csugl.h>
#include "ecs.h"

struct CloudController;

struct CloudComponent : public Component
{
    friend CloudController;
    CloudComponent(float freq, float phase, float amplitude)
        : freq(freq), phase(phase), amplitude(amplitude) {}
    float freq, phase, amplitude;

private:
    float _y;
};

struct CloudController : public System
{
    CloudController(csugl::Ref<Scene> cur_scene) : System(cur_scene) {}

    void start() override
    {
        _cur_scene->ComponentEach<CloudComponent, TransformComponent>(
            [](CloudComponent &cloud_c, auto &trans_c)
            {
                cloud_c._y = trans_c.transform.position.y;
                trans_c.transform.position.y = cloud_c._y + glm::sin(cloud_c.freq * (csugl::LowpTime::currentTime() + cloud_c.phase)) * cloud_c.amplitude;
            });
    }

    void update() override
    {
        _cur_scene->ComponentEach<CloudComponent, TransformComponent>(
            [](const CloudComponent &cloud_c, auto &trans_c)
            {
                trans_c.transform.position.y = cloud_c._y + glm::sin(cloud_c.freq * (csugl::LowpTime::currentTime() + cloud_c.phase)) * cloud_c.amplitude;
            });
    }
};

#endif